var express = require('express');
var router = express.Router();

var periodicalsTable = function(req, res) {
    req.pool.acquire(function(err, conn){
        conn.call('periodicals.getPeriodicals', {}, function(err, result) {
            req.model.page = 0;
            req.model.data = result;
            res.render('periodicals', req.model);
            req.pool.release(conn);
        });
    });
}

router.all('/(|page:id)', function(req, res, next) {
    req.pool.acquire(function(err, conn){
        conn.call('periodicals.getPeriodicalsPagesCount', {}, function(err, result) {
            req.model.pages = result.count;
            req.pool.release(conn);
            next();
        });
    });
});

router.get('/', periodicalsTable);

router.get('/page:id', function(req, res) {
    req.pool.acquire(function(err, conn){

        req.model.page = parseInt(req.params.id);
        if(isNaN(req.model.page)) req.model.page = 0;
        if(req.model.page < 0)  req.model.page = 0;

        conn.call('periodicals.getPeriodicals', {page: req.model.page}, function(err, result) {
            req.pool.release(conn);
            req.model.data = result;
            res.render('periodicals', req.model);
        });
    });
});

router.post('/create', function(req, res) {
    req.pool.acquire(function(err, conn){
        var params = {};
        params.title = req.body.title;
        params.opened = req.body.opened;
        params.type_title = req.body.type_title;

        conn.call('periodicals.createPeriodical', params, function(err, result) {
            req.pool.release(conn);

            if(result.error) {
                req.model.dangerAlerts.push(result.error);
            } else if(result.success) {
                req.model.successAlerts.push('Successfully created');
            }

            periodicalsTable(req, res);
        });
    });
});

router.all('/:id(\\d+)/?*', function(req, res, next) {
    var id = parseInt(req.params.id);
    if(!isNaN(id)) {
        req.model.p_id = id;
        next();
    } else {
        req.model.warningAlerts.push('wrong id');
        req.session.model = req.model;
        res.redirect("/Periodicals/");
    }
});

router.post('/:id(\\d+)/update', function(req, res) {
    var params = {};
    params.p_id = req.model.p_id;
    params.p_title = req.body.title;
    params.p_opened = req.body.opened;
    params.p_closed = req.body.closed;
    params.t_title = req.body.type;

    req.pool.acquire(function(err, conn) {
        conn.call('periodicals.updatePeriodical', params, function(err, result) {
            req.pool.release(conn);

            if(result.error) {
                req.model.dangerAlerts.push(result.error);
            } else {
                req.model.successAlerts.push('Succesfully updated');
            }

            req.session.model = req.model;
            res.redirect('/Periodicals/' + params.p_id);
        });
    });
});

router.get('/:id(\\d+)/delete', function(req, res) {
    req.pool.acquire(function(err, conn){
        conn.call('periodicals.deletePeriodical', {periodical_id: req.model.p_id}, function(err, result) {
            req.pool.release(conn);
            if(result.error) {
                req.model.dangerAlerts.push(result.error);
            } else {
                req.model.successAlerts.push('Succesfully deleted');
            }
            periodicalsTable(req, res);
        });
    });
});

router.get('/:id(\\d+)/issue/:i_id(\\d+)/delete', function(req, res) {
    var i_id = parseInt(req.params.i_id);
    req.pool.acquire(function(err, conn) {
        conn.call('issues.deleteIssue', {i_id: i_id}, function(err, result) {
            req.pool.release(conn);
            if(result.error) {
                req.model.dangerAlerts.push(result.error);
            } else {
                req.model.successAlerts.push('Successfully deleted');
            }
            req.session.model = req.model;
            res.redirect('/Periodicals/' + req.model.p_id );
        });
    });
});

router.post('/:id(\\d+)/issue/create', function(req, res) {
    var params = {};
    params.p_id = req.model.p_id;
    params.i_num = req.body.num;
    params.i_r_date = req.body.release_date;

    req.pool.acquire(function(err, conn) {
        conn.call('issues.createIssue', params, function(err, result) {
            req.pool.release(conn);
            if(result.error) {
                req.model.dangerAlerts.push(result.error);
            } else {
                req.model.successAlerts.push('Successfully created');
            }
            req.session.model = req.model;
            res.redirect('/Periodicals/' + req.model.p_id );
        });
    });
});

router.get('/:id(\\d+)/?*', function(req, res, next) {
    req.pool.acquire(function (err, conn) {
        conn.call('issues.getIssuesPagesCount', {p_id: req.model.p_id}, function(err, result) {
            req.pool.release(conn);
            if(result.error) {
                req.model.warningAlerts.push(result.error);
                req.model.pages = 0;
            } else {
                req.model.pages = result.count;
            }
            next();
        });
    });
});

router.get('/:id(\\d+)/?*', function(req, res, next) {
    req.model.page = 0;
    req.pool.acquire(function (err, conn) {
        conn.call('issues.getIssues', {p_id: req.model.p_id}, function(err, result) {
            req.pool.release(conn);
            if(result.error) {
                req.model.warningAlerts.push(result.error);
                req.model.issues = [];
            } else {
                req.model.issues = result;
            }
            next();
        });
    });
});

router.get('/:id(\\d+)/page:pg(\\d+)', function(req, res, next) {
    req.pool.acquire(function (err, conn) {
        req.model.page = parseInt(req.params.pg);
        if(isNaN(req.model.page)) req.model.page = 0;
        conn.call('issues.getIssues', {p_id: req.model.p_id, page: req.model.page}, function(err, result) {
            req.pool.release(conn);
            if(result.error) {
                req.model.warningAlerts.push(result.error);
                req.model.issues = [];
            } else {
                req.model.issues = result;
            }
            next();
        });
    });
});

router.get('/:id(\\d+)/?*', function(req, res) {
    req.pool.acquire(function (err, conn) {
        conn.call('periodicals.getPeriodicalInfo', {p_id: req.model.p_id}, function(err, result) {
            req.pool.release(conn);
            if(result.error) {
                req.model.warningAlerts.push(result.error);
                req.session.model = req.model;
                res.redirect("/Periodicals");
            } else {
                req.model.data = result;
                res.render('periodical', req.model);
            }
        });
    });
});

module.exports = router;
